/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年11月22日 10:21:39
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.DashWidgetConfig;
import com.pactera.jep.sys.service.DashWidgetConfigService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api("组件设计")
@RestController
@RequestMapping("/dashWidgetConfig")
public class DashWidgetConfigRestController extends CrudController<DashWidgetConfig>{
	private static Logger	logger	= LoggerFactory.getLogger(DashWidgetConfigRestController.class);

	@Autowired
	private DashWidgetConfigService	dashWidgetConfigService;


	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(Page<DashWidgetConfig> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		
		dashWidgetConfigService.query(page, filters, orders);
	}

	/**
	 * 保存
	 * @param dashWidgetConfig
	 * @return
	 */
	@ApiOperation("保存")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody DashWidgetConfig dashWidgetConfig) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		Boolean isExist = dashWidgetConfigService.exists(dashWidgetConfig.getWidgetCode());
		try 
		{
			if(isExist)
			{
				
				dashWidgetConfigService.update(dashWidgetConfig);
			}
			else
			{
				dashWidgetConfigService.insert(dashWidgetConfig);
			}
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param widgetCode
	 * @return
	 */
	@ApiOperation("删除")
	@PostMapping(value = "/remove/{widgetCode}")
	public Map<String, Object> remove(@PathVariable String widgetCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			dashWidgetConfigService.deleteByPrimaryKey(widgetCode);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 验证提交的widgetCode是否已经存在数据库中
	 * @param widgetCode
	 * @return
	 */
	@RequestMapping(value = "/check", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> check(String widgetCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		boolean exist = dashWidgetConfigService.exists(widgetCode);
		tips.put("valid", !exist);
		
		return tips;
	}
	
	/**
	 * 根据组件编码查询出组件设计
	 * @param widgetCode
	 * @returnw
	 */
	@ApiOperation("根据组件编码查询出组件设计")
	@PostMapping("/getWidgetConfigByWidgetCode/{widgetCode}")
	public Map<String,Object> getWidgetConfigByWidgetCode(@PathVariable String widgetCode){
		Map<String, Object> data = new HashMap<String, Object>();
		DashWidgetConfig dashWidgetConfig = dashWidgetConfigService.get(widgetCode);
		if(dashWidgetConfig!=null){
			data.put("dashWidgetConfig", dashWidgetConfig);
			data.put(WebCode.SUCCESS,true);
		}
		else
		{
			data.put(WebCode.SUCCESS,false);
		}
		return data;
	}
	
	
}